import requests
from lxml import etree

def do_requset(url):
    response = requests.get(url)
    # 编码方式
    response.encoding = "utf-8"
    return response.text
def parse_page(html):
    # 生成节点
    node = etree.HTML(html)
    titles = node.xpath("//div[@class='list-item']//a/text()")
    # 去掉空格
    titles = [str(i).strip() for i in titles]
    return titles
def create_url(start_page,end_page):
    base_url = "https://www.hbjcxy.com/jyyw/"
    for i in range(start_page-1,end_page):
        if i==0:
            url = base_url + "index.html"
            html = do_requset(url)
            titles = parse_page(html)
            print(titles)
            save_filename(str(i)+".txt",str(titles))
        else:
            url = base_url + "index_" + str(i + 1) + ".html"
            html = do_requset(url)
            titles = parse_page(html)
            print(titles)
            save_filename(str(i) + ".txt", str(titles))
def save_filename(filename,content):
    with open("标题/"+filename,"w+",encoding="utf-8") as f:
        f.write(content)
if __name__ == '__main__':
    create_url(1,5)